package com.simple.elpmobile.onlinecase.common.phone;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.iflytek.elpmobile.utils.Logger;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.telephony.TelephonyManager;


/**
 * 提供终端属性，所有跟终端属性相关的内容提供者
 * 
 * @author jgzheng
 */
public abstract class PhoneInfo
{

    /**
     * 日志TAG
     */
    private static final String TAG = "PhoneInfo";

    /**
     * 显示的宽度
     */
    private static int displayWidth = 0;

    /**
     * 显示的高度
     */
    private static int displayHeight = 0;

    /**
     * 状态栏高度
     */
    private static int statusBarHeight = 0;

    /**
     * 设置显示的矩阵大小
     * 
     * @param width 宽
     * @param height 高
     */
    public static void setDisplayMatrics(final int width, final int height)
    {
        displayWidth = width;
        displayHeight = height;
    }

    /**
     * 获取终端显示矩阵的宽度
     * 
     * @return 宽度
     */
    public static int getDeviceMatricsWidth()
    {
        return displayWidth;
    }

    /**
     * 获取终端显示矩阵的高度
     * 
     * @return 高度
     */
    public static int getDeviceMatricsHeight()
    {
        return displayHeight;
    }

    /**
     * 获取系统状态栏高度
     * 
     * @return 状态栏高度
     */
    public static int getStatusBarHeight()
    {
        return statusBarHeight;
    }

    /**
     * 设置系统状态栏高度
     * 
     * @param activity 页面
     */
    public static void setStatusBarHeight(final Activity activity)
    {
        if (statusBarHeight == 0)
        {
            final Rect frame = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(
                    frame);
            int height = frame.top;
            if (height <= 0)
            {
                height = Math.round(dip2px(activity, 25));
            }

            statusBarHeight = height;
        }
    }

    /**
     * 获取终端的IMEI标识号
     * 
     * @param context 上下文
     * @return 终端标识号IMEI
     */
    public static String getIMEI(final Context context)
    {
        return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    }

    /**
     * 获取终端SIM卡号码
     * 
     * @param context 上下文
     * @return 终端SIM卡号码
     */
    public static String getLine1Number(final Context context)
    {
        return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
    }

    /**
     * 获取终端的型号
     * 
     * @return 终端型号
     */
    public static String getDeviceModel()
    {
        return android.os.Build.MODEL;
    }

    /**
     * 获取终端的版本号
     * 
     * @return 终端版本号
     */
    public static String getOSRelease()
    {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * 获取终端内存大小
     * 
     * @return 终端内存大小
     */
    public static int getTotalMemory()
    {
        int result = 0;
        BufferedReader reader = null;
        InputStreamReader isReader = null;

        try
        {
            final String[] args = {"/system/bin/cat", "/proc/meminfo"};
            final ProcessBuilder cmd = new ProcessBuilder(args);
            final Process process = cmd.start();
            isReader = new InputStreamReader(process.getInputStream());
            reader = new BufferedReader(isReader);
            String line = "";

            for (int i = 1; (line = reader.readLine()) != null; i += 1)
            {
                if (line.contains("MemTotal"))
                {
                    result = Integer.parseInt(line.replaceAll("\\D+", ""));
                    result = result >> 10;
                    break;
                }
            }

        }
        catch (Exception e)
        {
            Logger.w(TAG, "[getTotalMemory] exception=" + e.getMessage());
        }
        finally
        {
            if (reader != null)
            {
                try
                {
                    reader.close();
                }
                catch (Exception e)
                {
                    Logger.w(TAG, "[getTotalMemory] exception="
                            + e.getMessage());
                }
            }
            if (isReader != null)
            {
                try
                {
                    isReader.close();
                }
                catch (Exception e)
                {
                    Logger.w(TAG, "[getTotalMemory] exception="
                            + e.getMessage());
                }
            }
        }

        return result;
    }

    /**
     * 获取终端CPU内存速度
     * 
     * @return 终端cpu内存速度
     */
    public static int getCPUSpeed()
    {
        int result = 0;
        BufferedReader reader = null;
        InputStreamReader isReader = null;

        try
        {
            final String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
            final ProcessBuilder cmd = new ProcessBuilder(args);
            final Process process = cmd.start();
            isReader = new InputStreamReader(process.getInputStream());
            reader = new BufferedReader(isReader);
            String line = "";

            for (int i = 1; (line = reader.readLine()) != null; i += 1)
            {
                if (line.contains("BogoMIPS"))
                {
                    result = (int) Float.parseFloat(line.replaceAll(
                            "[[\\D+]&&[^.]]", ""));
                    break;
                }
            }
        }
        catch (Exception e)
        {
            Logger.w(TAG, "[getCpuSpeed] exception=" + e.getMessage());
        }
        finally
        {
            if (reader != null)
            {
                try
                {
                    reader.close();
                }
                catch (Exception e)
                {
                    Logger.w(TAG, "[getCpuSpeed] exception=" + e.getMessage());
                }
            }
            if (isReader != null)
            {
                try
                {
                    isReader.close();
                }
                catch (Exception e)
                {
                    Logger.w(TAG, "[getCpuSpeed] exception=" + e.getMessage());
                }
            }
        }

        return result;
    }
    /** 
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
     */  
    public static int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }  
  
    /** 
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
     */  
    public static int px2dip(Context context, float pxValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (pxValue / scale + 0.5f);  
    }  

}
